package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.Dep;
import com.woniuxy.factory.entity.DepDTO;
import com.woniuxy.factory.entity.Emp;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.mapper.DepMapper;
import com.woniuxy.factory.mapper.EmpMapper;
import com.woniuxy.factory.mapper.PostMapper;
import com.woniuxy.factory.service.DepService;
import com.woniuxy.factory.service.EmpService;
import com.woniuxy.factory.util.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * @NTLX data 2020 08 Settings.DEPUTY_DIRECTOR
 */
@Service
public class DepServiceImp implements DepService {

    @Autowired
    DepMapper depMapper;
    @Autowired
    EmpMapper empMapper;
    @Autowired
    PostMapper postMapper;
    @Autowired
    EmpService empService;

    @Override
    public String depId() {
        String result = null;
        Integer depcount = depMapper.countDep() + 1;
        //设定字符串
        String str = "000";
        //截取 str长度-数字位数的"0"
        result = "DP" + str.substring(0, str.length() - depcount.toString().length()) + depcount;
        return result;
    }

    @Override
    @Transactional
    public ResponseResult addDep(Dep dep, String leader, String[] dleader) {
        //初始化参数
        Emp realLeader;
        Emp realDLeader;
        ResponseResult<String> result = new ResponseResult<>();
        //判断编号是否重复
        if (depMapper.selectDepById(dep.getDepId()) != null) {
            result.setCode(1);
            result.setMsg("编号已存在");
            return result;
        }
        //判断名称是否重复
        if (depMapper.selectDepByName(dep.getDepName()) != null) {
            result.setCode(1);
            result.setMsg("名称已存在");
            return result;
        }
        try {
            //判断主任和副主任是否为同一个人
            for (String str : dleader) {
                if (str.equals(leader))
                    throw new RuntimeException("主任和副主任不可为同一人");
            }
        } catch (NullPointerException e) {}
        //判断是否直接任命负责人
        if (leader != null && leader != "") {
            //判断所任命的人是否存在
            if ((realLeader = empMapper.selectEmpById(leader)) == null) {
                result.setCode(1);
                result.setMsg("任命了不存在的员工");
                return result;
            }
            //查询新部门的序号
            int index = depMapper.selectNewDepNum();
            dep.setDepNum(index);
            //调用员工岗位调动方法
            empService.transferEmp(realLeader, dep, postMapper.selectPostByPostRank(Settings.DIRECTOR));//查询部门主任岗位
        }
        //判断是否直接任命副负责人
        if (dleader != null) {
            //遍历所有副负责人的编号
            for (String str : dleader) {
                if ((realDLeader = empMapper.selectEmpById(str)) == null) {
                    result.setCode(1);
                    result.setMsg("任命了不存在的员工");
                    return result;
                }
                //查询新部门的序号
                int index = depMapper.selectNewDepNum();
                dep.setDepNum(index);
                //调用员工岗位调动方法
                empService.transferEmp(realDLeader, dep, postMapper.selectPostByPostRank(Settings.DEPUTY_DIRECTOR));//查询部门副主任岗位
            }
        }
        //判断新部门插入数据库是否成功
        if (depMapper.insertDep(dep) == 0) {
            throw new RuntimeException("存储失败，联系管理人员");
        }
        result.setCode(0);
        result.setMsg("添加成功");
        return result;
    }

    @Override
    public List<Dep> selectAllDep() {
        return depMapper.selectAllDep();
    }

    @Override
    public Dep selectDepById(String depId) {
        return depMapper.selectDepById(depId);
    }

    @Override
    public int deleteDep(String depId) {
        return depMapper.sDeleteDep(depId);
    }

    @Override
    public Dep selectDepByName(String depName) {
        return depMapper.selectDepByName(depName);
    }

    @Override
    public ResponseResult showAllDep() {
        ResponseResult<List> result = new ResponseResult<List>();
        List deps = new ArrayList();
        //负责人查询条件
        Emp leaderCondition = new Emp();
        leaderCondition.setPost(postMapper.selectPostByPostRank(Settings.DIRECTOR));
        //副负责人查询条件
        Emp dleaderCondition = new Emp();
        dleaderCondition.setPost(postMapper.selectPostByPostRank(Settings.DEPUTY_DIRECTOR));
        //查询部门信息（编号，名称，负责人，人数等）
        for (Dep dep : selectAllDep()) {
            //赋值部门给查询条件
            leaderCondition.setDep(dep);
            dleaderCondition.setDep(dep);
            //新建部门主任容器
            Emp leader = new Emp();
            //姓名赋值为""防止前端空指针报错
            leader.setEmpName("——");
            //查询主任结果
            List<Emp> list = empService.selectEmp(leaderCondition);
            //判断是否位非空
            if (list.size() > 0)
                leader = list.get(0);
            deps.add(new DepDTO(dep, leader, empService.selectEmp(dleaderCondition), empService.selectCountByDep(dep.getDepNum())));
        }
        //返回对象赋值
        result.setData(deps);
        result.setCode(0);
        return result;
    }

    @Override
    public ResponseResult<DepDTO> getDepInfo(String depId) {
        //初始化对象
        ResponseResult<DepDTO> result = new ResponseResult<>();
        DepDTO dto = new DepDTO();
        //查询部门信息
        dto.setDep(depMapper.selectDepById(depId));
        //部门主任查询条件
        Emp leaderCondition = new Emp();
        leaderCondition.setPost(postMapper.selectPostByPostRank(Settings.DIRECTOR));
        leaderCondition.setDep(dto.getDep());
        //新建部门主任容器
        Emp leader = new Emp();
        //姓名赋值为""防止前端空指针报错
        leader.setEmpName("");
        //查询主任结果
        List<Emp> list = empService.selectEmp(leaderCondition);
        //判断是否位非空
        if (list.size() > 0)
            leader = list.get(0);
        dto.setLeader(leader);
        //部门副主任查询条件
        Emp dleaderCondition = new Emp();
        dleaderCondition.setPost(postMapper.selectPostByPostRank(Settings.DEPUTY_DIRECTOR));
        dleaderCondition.setDep(dto.getDep());
        //查询副主任信息
        dto.setDleader(empMapper.selectEmpByCondition(dleaderCondition));
        result.setCode(0);
        result.setData(dto);
        return result;
    }

    @Override
    public ResponseResult editDep(Dep dep, String leader, String[] dleader) {
        //初始化参数
        Emp realLeader;
        Emp realDLeader;
        ResponseResult<String> result = new ResponseResult<>();
        //判断名称是否重复
        if (!depMapper.selectDepById(dep.getDepId()).getDepName().equals(dep.getDepName()) && depMapper.selectDepByName(dep.getDepName()) != null) {
            result.setCode(1);
            result.setMsg("名称已存在");
            return result;
        }
        try {
            //判断主任和副主任是否为同一个人
            for (String str : dleader) {
                if (str.equals(leader))
                    throw new RuntimeException("主任和副主任不可为同一人");
            }
        } catch (NullPointerException e) {}
        //查询部门序号
        int index = depMapper.selectDepById(dep.getDepId()).getDepNum();
        //判断是否直接任命负责人
        if (leader != null && leader != "") {
            //判断所任命的人是否存在
            if ((realLeader = empMapper.selectEmpById(leader)) == null) {
                result.setCode(1);
                result.setMsg("任命了不存在的员工");
                return result;
            }
            dep.setDepNum(index);
            //判断是否主任是否没变化
            if (!realLeader.getDep().getDepId().equals(dep.getDepId()) || realLeader.getPost().getpostRank() != Settings.DIRECTOR)
                //有变化则调用员工岗位调动方法
                empService.transferEmp(realLeader, dep, postMapper.selectPostByPostRank(Settings.DIRECTOR));//查询部门主任岗位
        }
        //判断是否直接任命副负责人
        if (dleader != null) {
            //遍历所有副负责人的编号
            for (String str : dleader) {
                if ((realDLeader = empMapper.selectEmpById(str)) == null) {
                    result.setCode(1);
                    result.setMsg("任命了不存在的员工");
                    return result;
                }
                dep.setDepNum(index);
                if (!realDLeader.getDep().getDepId().equals(dep.getDepId()) || realDLeader.getPost().getpostRank() != Settings.DEPUTY_DIRECTOR)
                    //有变化调用员工岗位调动方法
                    empService.transferEmp(realDLeader, dep, postMapper.selectPostByPostRank(Settings.DEPUTY_DIRECTOR));//查询部门副主任岗位
            }
        }
        //判断部门修改数据库是否成功
        if (depMapper.updateDep(dep) == 0) {
            throw new RuntimeException("存储失败，联系管理人员");
        }
        result.setCode(0);
        result.setMsg("修改成功");
        return result;
    }

}
